package com.naihe.anderson.sms.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.manager.NhSmsMgr;
import com.naihe.anderson.sms.util.LockPatternUtils;
import com.naihe.anderson.sms.widgets.LockPatternView;
import com.naihe.anderson.sms.widgets.LockPatternView.Cell;

public class ConfirmPasswordActivity extends Activity implements OnClickListener {

	public NhSmsApp nhSmsApp;
	public NhSmsMgr nhSmsMgr;
	private String passwordCurrent;

	private Button btnSpLeft, btnSpRight;
	private TextView TopText;
	private LockPatternView mLockPatternView;

	private String currentPassword = "";

	private String inentAction;
	private static final int MSG_WHAT_CLEARPATTERN = 0;
	private static final int MSG_WHAT_FINISH = 1;
	private static final int MSG_WHAT_WRONG_FINISH = 2;

	private int passwordWrongCount = 0;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_CLEARPATTERN:
				mLockPatternView.clearPattern();
				break;
			case MSG_WHAT_FINISH:
				Intent intent = null;
				if(inentAction.equals("BaseSetActivity")){
					nhSmsMgr.setBaseSafetyCipher("");
					intent = new Intent(ConfirmPasswordActivity.this, BaseSetActivity.class);
				} else if(inentAction.equals("MainSmsActivity")){
					intent = new Intent(ConfirmPasswordActivity.this, MainSmsActivity.class);
				}
				ConfirmPasswordActivity.this.setResult(RESULT_OK, intent);
				ConfirmPasswordActivity.this.finish();
				break;
			case MSG_WHAT_WRONG_FINISH:
				ConfirmPasswordActivity.this.finish();
				break;
			default:
				break;
			}
		}
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setpassword);

		inentAction = getIntent().getStringExtra("inentAction");
		nhSmsApp = (NhSmsApp) getApplication();
		nhSmsMgr = nhSmsApp.getNhSmsMgr();

		btnSpLeft = (Button) findViewById(R.id.btnSpLeft);
		btnSpRight = (Button) findViewById(R.id.btnSpRight);
		TopText = (TextView) findViewById(R.id.SpTopText);
		mLockPatternView = (LockPatternView) findViewById(R.id.SpLockPattern);
		mLockPatternView.setInStealthMode(nhSmsMgr.getBaseCipherViewVisible());
		mLockPatternView.setTactileFeedbackEnabled(nhSmsMgr.getBaseCipherViewVibor());
		
		TopText.setText(R.string.pattern_to_draw);
		currentPassword = nhSmsMgr.getBaseSafetyCipher();

		btnSpLeft.setOnClickListener(this);
		btnSpRight.setOnClickListener(this);

		mLockPatternView.setOnPatternListener(new LockPatternView.OnPatternListener() {
			public void onPatternStart() {
				TopText.setText(R.string.pattern_start);
			}

			public void onPatternCleared() {

			}

			public void onPatternCellAdded(List<Cell> pattern) {
			}

			public void onPatternDetected(List<Cell> pattern) {
				if (pattern.size() >= 4) {
					mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Correct);
					passwordCurrent = LockPatternUtils.patternToString(pattern);
					if(passwordCurrent.equals(currentPassword)){
						mHandler.sendEmptyMessage(MSG_WHAT_FINISH);
					} else {
						if(passwordWrongCount < 3){
							TopText.setText("密码错误");
							mHandler.sendEmptyMessageDelayed(MSG_WHAT_CLEARPATTERN, 800);
						} else {
							TopText.setText("密码错误超过3次");
							mHandler.sendEmptyMessageDelayed(MSG_WHAT_WRONG_FINISH, 300);
						}
						passwordWrongCount ++;
					}
				} else {
					TopText.setText(R.string.patternlimit);
					mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
					mHandler.sendEmptyMessageDelayed(MSG_WHAT_CLEARPATTERN, 800);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnSpRight) {
			mHandler.sendEmptyMessage(MSG_WHAT_WRONG_FINISH);
		} else if (v.getId() == R.id.btnSpLeft) {
			mHandler.sendEmptyMessage(MSG_WHAT_WRONG_FINISH);
		}
	}

}
